<template>
	<div
		:class="{
			'app-control-portlet__view': true,
			[`app-control-portlet__view__${name}`]: true,
		}"
	>
		<component
			:is="viewName"
			:context="drContext"
			:viewParams="drViewParams"
			:name="viewName"
			openType="EMBED"
			:isLoadDefault="false"
			@view-action="(name: string, action: IEvent<string>, data: IParam) => { handleViewAction(name, action, data); }"
			@view-init="(name: string, data: IViewAbility) => { handleViewInit(name, data); }"
			@view-destroy="(name: string, data: IParam) => { handleViewDestroy(name, data); }"
		/>
	</div>
</template>
<script setup lang="ts">
import { IEvent, computedNavData, IParam, IViewAbility } from '@/core';
import { Ref } from 'vue';
import { portletViewProps } from '../portlet';
const props = defineProps(portletViewProps);
interface ComponentEmit {
	(name: 'component-action', value: IEvent<string>): void;
}
const emit = defineEmits<ComponentEmit>();
// 关系界面上下文
const drContext: Ref<any> = ref({});
// 关系界面视图参数
const drViewParams: Ref<any> = ref({});

// 刷新节点
let hookItems: any[] = [];
// 关系界面能力
let ability: any = null;

const refreshPortletView = (data: IParam, action: string) => {
	let tempContext: any = {};
	let tempParam: any = {};
	const viewConfig = App.getAppViewConfig()[props.viewName];
	let parameters: any[] = [];
	if (viewConfig) {
		parameters = [...viewConfig.parameters];
	}
	parameters.forEach((parameter: any) => {
		const {
			pathName,
			parameterName,
		}: { pathName: string; parameterName: string } = parameter;
		if (data[parameterName] && !Object.is(data[parameterName], '')) {
			Object.assign(tempContext, {
				[parameterName]: data[parameterName],
			});
		}
	});
	Object.assign(tempContext, props.context);
	drContext.value = tempContext;
	if (props.viewParams.hasOwnProperty('copymode')) {
		Object.assign(tempParam, { copymode: props.viewParams.copymode });
	}
	drViewParams.value = tempParam;
	if (ability) {
		if (action == 'load') {
			ability.load();
		}
		if (action == 'refresh') {
			ability.refresh();
		}
	}
};

const savePortletView = (data: IParam) => {
	if (props.tempMode && props.tempMode == 2) {
		if (ability) {
			ability.saveView();
		}
	} else {
		if (data && !Object.is(data.srfuf, '0')) {
			if (ability) {
				ability.saveView();
			}
		} else {
			emit('component-action', {
				name: props.name,
				action: 'viewDataSave',
				data,
			});
		}
	}
};

const initPortletView = () => {
	const executePortletView = ({ name, action, data }: IEvent<string>) => {
		switch (action) {
			case 'load':
				refreshPortletView(data, 'load');
				break;
			case 'valueChange':
			case 'save':
				refreshPortletView(data, 'refresh');
				break;
			case 'beforeSave':
				savePortletView(data);
				break;
			default:
				break;
		}
	};
	// emit('component-action', { name: props.name, action: 'componentInit', data: { executePortletView: executePortletView } });
};

initPortletView();

const handleViewAction = (
	name: string,
	action: IEvent<string>,
	data: IParam
) => {
	console.log('视图行为', name, action, data);
	if (Object.is(action, 'viewDataSave')) {
		emit('component-action', {
			name: props.name,
			action: 'viewDataSave',
			data,
		});
	}
};
const handleViewInit = (name: string, data: IViewAbility) => {
	console.log('视图初始化', name, data);
	ability = data;
	emit('component-action', {
		name: props.name,
		action: 'viewInit',
		data: ability,
	});
};
const handleViewDestroy = (name: string, data: IParam) => {
	console.log('视图销毁', name, data);
};
</script>
